package com.winshare.saleAssistant.service.impl;

import com.winshare.saleAssistant.dao.AppRoleMapper;
import com.winshare.saleAssistant.domain.dto.req.*;
import com.winshare.saleAssistant.domain.dto.resp.AppRoleRespDTO;
import com.winshare.saleAssistant.domain.entity.AppRoleDO;
import com.winshare.saleAssistant.domain.entity.AppRoleRelationDO;
import com.winshare.saleAssistant.domain.query.AppRoleQuery;
import com.winshare.saleAssistant.exception.BusinessException;
import com.winshare.saleAssistant.mapper.AppRoleMapStruct;
import com.winshare.saleAssistant.service.AppRoleService;
import com.winshare.saleAssistant.util.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AppRoleServiceImpl implements AppRoleService {

    private final AppRoleMapper appRoleMapper;

    public AppRoleServiceImpl(AppRoleMapper appRoleMapper) {
        this.appRoleMapper = appRoleMapper;
    }

    @Override
    public void saveAppRole(AppRoleCreateDTO appRoleCreateDTO) {
        AppRoleDO appRoleDO = AppRoleMapStruct.INSTANCE.fromAppRoleCreateDTO(appRoleCreateDTO);
        if(checkAppRoleExist(appRoleDO)) {
            throw new BusinessException("此应用角色名称在系统中已存在");
        }
        appRoleMapper.saveAppRole(appRoleDO);
        List<AppDTO> appDTOList = appRoleCreateDTO.getAppList();
        if(!CollectionUtils.isEmpty(appDTOList)) {
            AppRoleDO appRoleDOInSql = appRoleMapper.queryAppRole(appRoleDO);
            List<AppRoleRelationDO> appRoleRelationList = appDTOList.stream().map(item -> {
                AppRoleRelationDO appRoleRelationDO = new AppRoleRelationDO();
                appRoleRelationDO.setAppId(item.getId());
                appRoleRelationDO.setAppRoleId(appRoleDOInSql.getId());
                return appRoleRelationDO;
            }).collect(Collectors.toList());
            appRoleMapper.batchInsertAppRoleRelation(appRoleRelationList);
        }
    }

    @Override
    public void updateAppRole(AppRoleUpdateDTO appRoleUpdateDTO) {
        AppRoleDO appRoleDO = AppRoleMapStruct.INSTANCE.fromAppRoleUpdateDTO(appRoleUpdateDTO);
//        if(appRoleDO.getId() == 1) {
//            throw new BusinessException("超级管理员的应用角色不能随意修改");
//        }
        if(checkAppRoleExist(appRoleDO)) {
            throw new BusinessException("此应用角色名称在系统中已存在");
        }
        appRoleMapper.updateAppRole(appRoleDO);
        List<AppDTO> appDTOList = appRoleUpdateDTO.getAppList();
        if(!CollectionUtils.isEmpty(appDTOList)) {
            appRoleMapper.deleteRelationByAppRole(appRoleDO);
            List<AppRoleRelationDO> appRoleRelationDOList = appDTOList.stream().map(item -> {
                AppRoleRelationDO appRoleRelationDO = new AppRoleRelationDO();
                appRoleRelationDO.setAppId(item.getId());
                appRoleRelationDO.setAppRoleId(appRoleDO.getId());
                return appRoleRelationDO;
            }).collect(Collectors.toList());
            appRoleMapper.batchInsertAppRoleRelation(appRoleRelationDOList);
        }
    }

    @Override
    public Page<AppRoleRespDTO> getAppRoleList(AppRoleQueryDTO appRoleQueryDTO) {
        AppRoleQuery appRoleQuery = AppRoleMapStruct.INSTANCE.fromAppRoleQueryDTO(appRoleQueryDTO);
        appRoleQuery.setOffsetIndex(appRoleQueryDTO.getOffsetIndex());
        int totalCount = appRoleMapper.queryAppRoleCount(appRoleQuery);
        Integer pageIndex = appRoleQueryDTO.getPageIndex();
        Integer pageSize = appRoleQueryDTO.getPageSize();
        if(0 == totalCount) {
            return new Page<>(pageIndex,pageSize,totalCount,new ArrayList<>());
        }
        List<AppRoleDO> appRoleDOList = appRoleMapper.findAppRoleListByConditions(appRoleQuery);
        List<AppRoleRespDTO> appRoleRespDTOList = AppRoleMapStruct.INSTANCE.fromAppRoleDOList(appRoleDOList);
        return new Page<>(pageIndex,pageSize,totalCount,appRoleRespDTOList);
    }

    @Override
    public void deleteAppRole(BaseIdDTO baseIdDTO) {
        AppRoleDO appRoleDO = AppRoleMapStruct.INSTANCE.fromBaseIdDTO(baseIdDTO);
        AppRoleDO appRoleDOInSql = appRoleMapper.getAppRole(appRoleDO);
//        if(appRoleDO.getId() == 1) {
//            throw new BusinessException("超级管理员的应用角色不能随意修改");
//        }
        if(null == appRoleDOInSql) {
            throw new BusinessException("当前应用角色不存在。");
        }
        if(checkAppExistWithAppRole(appRoleDO)) {
            throw new BusinessException("当前应用角色下面还关联有应用");
        }
        if(checkStaffExistWithAppRole(appRoleDO)) {
            throw new BusinessException("当前应用角色下面还关联有职员");
        }
        appRoleMapper.deleteAppRole(appRoleDO);
    }

    @Override
    public AppRoleRespDTO getAppRoleDetail(BaseIdDTO baseIdDTO) {
        AppRoleDO appRoleDO = AppRoleMapStruct.INSTANCE.fromBaseIdDTO(baseIdDTO);
        AppRoleDO appRoleDOInSql = appRoleMapper.getAppRole(appRoleDO);
        if(null == appRoleDOInSql) {
            throw new BusinessException("当前应用角色不存在");
        }
        AppRoleRespDTO appRoleRespDTO = AppRoleMapStruct.INSTANCE.fromAppRoleDO(appRoleDOInSql);
        return appRoleRespDTO;
    }

    private boolean checkStaffExistWithAppRole(AppRoleDO appRoleDO) {
        return appRoleMapper.countStaffWithAppRole(appRoleDO) > 0 ;
    }

    private boolean checkAppExistWithAppRole(AppRoleDO appRoleDO) {
        return appRoleMapper.countAppWithAppRole(appRoleDO) > 0 ;
    }

    private boolean checkAppRoleExist(AppRoleDO appRoleDO) {
        return appRoleMapper.countByAppRoleName(appRoleDO) > 0;
    }
}
